package com.alibaba.org.service.impl;

import com.alibaba.basic.service.impl.BaseServiceImpl;
import com.alibaba.basic.uitl.BaiduAuditUtils;
import com.alibaba.basic.uitl.BusinessException;
import com.alibaba.org.domain.Employee;
import com.alibaba.org.domain.Shop;
import com.alibaba.org.domain.ShopAuditLog;
import com.alibaba.org.dto.ShopAdminDto;
import com.alibaba.org.dto.ShopAuditDto;
import com.alibaba.org.dto.ShopSettlementDto;
import com.alibaba.org.mapper.EmployeeMapper;
import com.alibaba.org.mapper.ShopAuditLogMapper;
import com.alibaba.org.mapper.ShopMapper;
import com.alibaba.org.service.IShopService;
import com.alibaba.user.mapper.LogininfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2023-02-28
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    // 我们添加日志需要一个Mapper
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private JavaMailSender javaMailSender;
    /**
     *
     * 店铺入驻
     *
     * */

    @Override
    public void settlement(ShopSettlementDto dto) {
    // 1.校验参数
    // 2.判断两次密码是否一致
        ShopAdminDto admin = dto.getAdmin();
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("两次密码不一样，重新给老子填");
        };
        // 3.店铺是否被注册
        Shop shop = shopMapper.findByName(dto.getName());
        if (null != shop) {
            throw new BusinessException("店铺已经被注册了，你还注册个灯，滚去直接登录傻逼");
        }
        // 4.管理员是否已经有门店了 ①在emp有没有数据 ②有没有shopIp ③shopId等于adminId
        Employee employee  = employeeMapper.findByName(admin.getUsername());
        if (null != employee && null != employee.getShopId()){ // 证明他是店铺的人
            Shop byAdminId = shopMapper.findByAdminId(employee.getId()); // 通过adminID去毕竟员工表的id
            if (null != byAdminId){ // 说明他是一个店长
                throw new BusinessException("你要弄几个店，生意有楞个好，滚去换个人");
            }

        }
        // 5. 百度AI nadao
        if (!BaiduAuditUtils.TextCensor(dto.getName())){
            throw new BusinessException("店铺名字违规");
        }
        if (!BaiduAuditUtils.ImgCensor(dto.getLogo())){
            throw new BusinessException("店铺logo违规");
        }
    // 6.添加数据
        // 添加店铺的信息 把dto的字段取出来赋值给shop对象
        shop = dto.getShop();
        shopMapper.add(shop);

        //生成logininfo
//        Logininfo logininfo = new Logininfo();
//        BeanUtils.copyProperties(shop, logininfo);
//        logininfo.setDisable(true);
//        logininfo.setType(0);
//        logininfoMapper.add(logininfo);
//        shop.setAdminId(logininfo.getId());
//        shopMapper.add(shop);
        // 添加店长的信息  把dto中的字段取出来 赋值给emp对象
        Employee emp = dto.getEmp();
        emp.setShopId(shop.getId());
        employeeMapper.add(emp);
        // shop里面没有adminid
        Shop shop1 = new Shop();
        shop1.setId(shop.getId());
        shop1.setAdminId(emp.getId());
        shopMapper.update(shop1);


    }


    @Override
    public void activity(Long id) {
        // 修改店铺状态
        Shop shop = new Shop();
        shop.setState(2);
        shop.setId(id);
        shopMapper.update(shop);

    }
    // 通过审批
    @Override
    public void auditPass(ShopAuditDto dto) {
        // 1 修改店铺状态
        Shop shop = new Shop();
        shop.setState(1);
        shop.setId(dto.getShopId());
        shopMapper.update(shop);
        // 2 添加审核日志
        ShopAuditLog log = new ShopAuditLog();
        log.setShopId(dto.getShopId());
        log.setAuditId(-1);
        log.setAuditTime(new Date());
        log.setNote(dto.getNote());
        shopAuditLogMapper.add(log);
        // 3 发送邮件
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("353974909@qq.com");
            helper.setSubject("宠物乐园审核结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>恭喜你审核通过：请点击下方链接激活店铺：</h1>"+
                    "http://localhost:8080/shop/activity/"+dto.getShopId(),true);
            // 4.添加附件
//            helper.addAttachment("好东西.jpg",new File("C:\\Users\\admin\\Desktop\\1.jpg"));
            // 5.设置收件人
            helper.setTo("353974909@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }



    }

    @Override
    public void auditReject(ShopAuditDto dto) {
        // 修改状态
        Shop shop = new Shop();
        shop.setState(3);
        shop.setId(dto.getShopId());
        shopMapper.update(shop);
        // 添加审核日志
        ShopAuditLog log = new ShopAuditLog();
        log.setShopId(dto.getShopId());
        log.setAuditId(-1);
        log.setAuditTime(new Date());
        log.setNote(dto.getNote());
        shopAuditLogMapper.add(log);
        // 发送邮件
        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            // 2.发送复杂邮件的工具类
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
            helper.setFrom("353974909@qq.com");
            helper.setSubject("宠物乐园审核结果通知");
            // 3.设置文本内容，第二个参数 true 代表是否是一个html内容
            helper.setText("<h1>不好意思，审核没通过，请点击下方链接重新审核</h1>"+
                    "http://localhost:8081/#/registerAgain?id="+dto.getShopId(),true);
            // 4.添加附件
//            helper.addAttachment("好东西.jpg",new File("C:\\Users\\admin\\Desktop\\1.jpg"));
            // 5.设置收件人
            helper.setTo("353974909@qq.com");
            // 6.发送邮件
            javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}
